﻿layui.use(['form', 'layedit', 'laydate'], function () {
    
    var form = layui.form
    , layedit = layui.layedit
    , laydate = layui.laydate
	, layer = parent.layer === undefined ? layui.layer : parent.layer
	, laypage = layui.laypage,
		$ = layui.jquery;


    //自定义验证规则
    form.verify({
        Names: function (value) {
            if (value.length <= 0) {
                return '职位名称不能为空';
            }
        }

    });

    function AddSubmit(data) {
       
        var ids = $('#deptSelect option:selected').val();//下拉框选中的值  id  传到控制器
        var pname = $('#namesp').val();//文本框的值 传到控制器
        $.ajax({
            type: "post",
            url: "/Adddepartment/PositionS",//方法
            data:
                {
                    "DepartmentId": ids,//对应实体数据库字段  新建实体类
                    "P_Name": pname //对应实体数据库字段

                },
            dataType: "json",
            success: function (result) {
                if (result.State == 1)
                    layer.alert(result.Message, { icon: 1, time: 1.5 * 1000 });
                else
                    layer.msg(result.Message, { icon: 7, time: 1.5 * 1000 });
            }
        });
    };


    //所属部门下拉框绑定
    function Select() {
       

        var url = "/Adddepartment/Selects2";
        $.post(url, function (result) {
            var departments = JSON.parse(result.data);
            var deptSelect = $("#deptSelect");
            layui.each(departments, function (index, data) {
                var deptOption = $("<option value='" + data.D_Id + "'>" + data.D_Name + "</option>");
                deptSelect.append(deptOption);
            });
            form.render();
        });
    };



    $(function () {
        Select();

        //监听提交  subForm为lay-filter属性
        form.on('submit(demo3)', function (data) {
        
            AddSubmit(data);

            return false;
        });
    });



});